<?php

namespace api\user\controller;

use cmf\controller\RestAdminBaseController;

class InfoController extends RestAdminBaseController
{
    //个人信息
    function info()
    {
        $data = [
            'user_nickname' => $this->user['user_nickname'],
            'mobile' => $this->user['mobile'],
            'user_email' => $this->user['user_email'],
            'sex' => $this->user['sex'],
            'birthday' => $this->user['birthday'] ? date('Y-m-d', $this->user['birthday']) : '未填写',
            'signature' => $this->user['signature'],
            'avatar' => cmf_get_image_url($this->user['avatar'] ?: '/static/images/headicon.png'),//$this->user['avatar'],
        ];
        $more = json_decode($this->user['more'], true);
        $data['wxcode'] = $more['wxcode'] ?? '';
        $data['address'] = $more['address'] ?? '';
        $this->success('success', $data);
    }

    // 管理员退出
    public function logout()
    {
        $userId = $this->getUserId();
        db('user_token')->where([
            'token' => $this->token,
            'user_id' => $userId,
            'device_type' => $this->deviceType
        ])->update(['token' => '']);

        $this->success("退出成功!");
    }

    //设置
    function setting()
    {
        $user = $this->user;
        $user['more'] = json_decode($user['more'], true);
        if ($this->request->post('wxcode')) {
            $user['more']['wxcode'] = $this->request->post('wxcode');
        }
        if ($this->request->post('address')) {
            $user['more']['address'] = $this->request->post('address');
        }
        if ($this->request->post('sex', 0, 'intval')) {
            $user['sex'] = $this->request->post('sex');
        }
        if ($this->request->post('birthday')) {
            $user['birthday'] = $this->request->post('birthday', '', 'strtotime');
        }
        if ($this->request->post('signature')) {
            $user['signature'] = $this->request->post('signature');
        }
        if ($this->request->file('avatar')) {
            $file = $this->request->file('avatar');
            $result = $file->validate([
                'ext' => 'jpg,jpeg,png',
                'size' => 1024 * 1024
            ])->move('..' . DS . 'upload' . DS . 'avatar' . DS);

            if ($result) {
                $avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
                $avatar = 'avatar/' . $avatarSaveName;

                $user['avatar'] = $avatar;
            } else {
                $this->error([
                    'code' => 10001,
                    'msg' => $file->getError()
                ]);
            }
        }

        $user['more'] = json_encode($user['more']);
        db('user')->where("id={$this->userId}")->update($user);

        $new = db('user')->find($this->userId);

        $data = [
            'user_nickname' => $new['user_nickname'],
            'mobile' => $new['mobile'],
            'user_email' => $new['user_email'],
            'sex' => $new['sex'],
            'birthday' => $new['birthday'] ? date('Y-m-d', $new['birthday']) : '未填写',
            'signature' => $new['signature'],
            'avatar' => cmf_get_image_url($new['avatar'] ?: '/static/images/headicon.png'),//$this->user['avatar'],
        ];
        $more = json_decode($new['more'], true);
        $data['wxcode'] = $more['wxcode'] ?? '';
        $data['address'] = $more['address'] ?? '';
        $this->success('设置成功', $data);
    }

    /**
     * 重置密码
     */
    function password()
    {
        $data = $this->request->param();
        if (empty($data['old_password'])) {
            $this->error([
                'code' => 10001,
                'msg' => '原始密码不能为空！',
            ]);
        }
        if (empty($data['password'])) {
            $this->error([
                'code' => 10001,
                'msg' => '新密码不能为空！',
            ]);
        }

        $oldPassword = $data['old_password'];
        $password    = $data['password'];
        $rePassword  = $data['re_password'];

        if ($password == $rePassword) {
            $userId = $this->userId;
            $admin = db('user')->find($userId);
            if (cmf_compare_password($oldPassword, $admin['user_pass'])) {
                if (cmf_compare_password($password, $admin['user_pass'])) {
                    $this->error([
                        'code' => 10001,
                        'msg' => '新密码不能和原始密码相同！',
                    ]);
                } else {
                    db('user')->where([
                        'id' => $userId
                    ])->update([
                        'user_pass' => cmf_password($password)
                    ]);
                    $this->success("密码修改成功！");
                }
            } else {
                $this->error([
                    'code' => 10001,
                    'msg' => '原始密码不正确！',
                ]);
            }
        } else {
            $this->error([
                'code' => 10001,
                'msg' => '密码输入不一致！',
            ]);
        }
    }
}